package com.meiyou.controller;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.meiyou.pojo.Tixian;
import com.meiyou.pojo.User;
import com.meiyou.service.TixianService;
import com.meiyou.utils.Msg;
import com.meiyou.utils.PagesMap;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api(value = "提现接口", tags = { "提现接口" })
@RequestMapping("/tixian")
public class TixianController {

	@Autowired
	TixianService tixianService;
	
	//用户申请提现
	@RequestMapping(value="/usertixian",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="用户提现",notes="用户提现",httpMethod="POST")
	public Msg usertixian(String tUCount,double tMeijin,String tFangshi) {
		//提现人 提现金币 提现方式
		Tixian tixian = new Tixian();
		tixian.settUCount(tUCount);
		tixian.settFangshi(tFangshi);
		tixian.settMeijin(tMeijin);
		tixian.settSdmeijin(tMeijin);
		tixian.settShenhe("待审核");
		Date date = new Date();
		tixian.settTime(date);
		
		return Msg.success(); 
	}
	
	//自动提现
	@RequestMapping(value="/usershenghetixian",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="管理员审核提现",notes="管理员审核提现",httpMethod="POST")
	public Msg usershenghetixian(String tUCount,double tMeijin,double Ymeijin) {
		//1.修改用户表中美金
		double Bmeijin = Ymeijin - tMeijin;
		User user = new User();
		user.setuPhone(tUCount); 
		user.setuMeijin(Bmeijin);
		tixianService.updateuserMeijin(user);
		//2.修改提现记录表中状态
		Tixian tixian = new Tixian();
		tixian.settUCount(tUCount);
		tixian.settShenhe("提现成功"); 
		tixianService.updateTixian(tixian);
		return Msg.success(); 
	}
	
	//查询提现记录
	@RequestMapping(value="/tixianjilu",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="提现记录",notes="提现记录",httpMethod="POST")
	public Msg tixianjilu(int pn,String strtime) {
		Tixian tixian = new Tixian();
		if(strtime!=null && strtime!="") {
			String s[] = strtime.split(",");
			tixian.setStarttime(s[0]);
			tixian.setEndtime(s[1]); 
		}
		List<Tixian> list = tixianService.selectTixian(tixian);		
		Map<String,Object> map = PagesMap.getpagesTixianMaps(list, pn);
		return Msg.success().add("pageInfo", map);  
	}
	
	//删除提现记录
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="删除提现记录",notes="删除提现记录",httpMethod="POST")
	public Msg delete(int id) {
		
	//	System.out.println(id);
		tixianService.delete(id);
		return Msg.success();
	}
	
	//根据ID查询信息
	@RequestMapping(value="/selectById",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="根据ID查询信息",notes="根据ID查询信息",httpMethod="POST")
	public Msg selectById(String id) {
		
		Tixian tixian = tixianService.selectById(id);
		return Msg.success().add("tixian", tixian); 
	}
	
	//管理员审核提现
	@RequestMapping(value="/shenghetixian",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="管理员审核提现",notes="管理员审核提现",httpMethod="POST")
	public Msg shenghetixian(String tUCount,double tMeijin,String phone) {
		
		System.out.println(tUCount+"||"+tMeijin+"||"+phone);
		//1、查询出用户金币
		double meijin = tixianService.selectMeijinById(phone);
		//2、判断用户金币是否够
		if(meijin<tMeijin) {
			return Msg.fail();
		}
		//3、修改用户表中美金
		double Bmeijin = meijin - tMeijin;
		User user = new User();
		user.setuPhone(phone); 
		user.setuMeijin(Bmeijin);
		tixianService.updateuserMeijin(user);
		//4、修改提现记录表中状态
		Tixian tixian = new Tixian();
		tixian.settUCount(tUCount);
		tixian.settShenhe("提现成功"); 
		tixianService.updateTixian(tixian);
		return Msg.success();
	}
}
